package com.taobao.org.controller;
import com.taobao.basic.exception.BusinessException;
import com.taobao.basic.util.ExcelUtils;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.PageList;
import com.taobao.org.domain.Shop;
import com.taobao.org.domain.ShopLog;
import com.taobao.org.dto.ShopDto;
import com.taobao.org.query.ShopQuery;
import com.taobao.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/shop")
@Api(value = "店铺接口类",description = "员工接口的CRUD操作")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * http://localhost:8080/Shop  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有店铺信息" )
    public List<Shop> findAll(){

        return shopService.loadAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/Shop/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Shop findOne(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            shopService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/Shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updateOrAdd(@RequestBody Shop Shop){

        try {
            if(Shop.getId()==null){
                // 添加操作
                shopService.save(Shop);
            }else{
                // 修改操作
                shopService.update(Shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 店铺入驻的
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) { // 业务问题
            e.printStackTrace();
            // 代码问题(其他不可抗拒因素)  // e.getMessage()： 获取的是你自定义的信息
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            // 代码问题(其他不可抗拒因素)
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            // 哈哈哈
            return shopService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Shop>();
        }
    }

    /**
     * 审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult pass(@RequestBody ShopLog log){

        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }


    /**
     * 驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "驳回" )
    public JsonResult reject(@RequestBody ShopLog log){

        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        try {
            // 查询所有shop数据
            List<Shop> list = shopService.loadAll();
            // d调用工具了  (List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response)
            ExcelUtils.exportExcel(list,null,"店铺数据",Shop.class,"aa.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 导入  ，调用工具类  MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass

            List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            // 保存到数据库表中
            list.forEach(System.out::println);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * domain，pojo.： 实体类----》跟数据库中的表对应的
     * query:  做查询参数使用
     * dto: 接收参数或者传参使用的
     * @return
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }

}

